//
//  GameViewController.m
//  Politics
//
//  Created by Hai Nguyen on 7/9/11.
//  Copyright 2011 VinaSource. All rights reserved.
//

#import "GameViewController.h"
#import "QuestionModel.h"
#import "AppDelegate_iPhone.h"
#import "QuestionModel.h"
#import "HomeGameViewController.h"
#import "EndGameViewController.h"

@implementation GameViewController

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{	
    [super viewDidLoad];
	if ([mainiPhoneAppDelegate.arrayGames count] > mainiPhoneAppDelegate.latestQuestionIndex ) 
	{
		QuestionModel *question = [mainiPhoneAppDelegate.arrayGames objectAtIndex:mainiPhoneAppDelegate.latestQuestionIndex];
		[self setupWithQuestion:question];
		mainiPhoneAppDelegate.latestQuestionIndex++;
	}
	keySelected = 0;
	isChecked = NO;
	expiredTime = 12;
	playTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateProgress) userInfo:nil repeats:YES];
}
- (void) updateProgress
{
	expiredTime--;
	if (expiredTime>0)
	{
		if (expiredTime < 4)
		{
			[timeLabel setTextColor:[UIColor redColor]];
		}
		[timeLabel setFrame:CGRectMake(timeLabel.frame.origin.x-16.51, timeLabel.frame.origin.y, timeLabel.frame.size.width, timeLabel.frame.size.height)];
		[timeLabel setText:[NSString stringWithFormat:@"%ds",expiredTime]];
		timeProgressView.progress = timeProgressView.progress - 0.083;
	}
	else
	{
		[playTimer invalidate];
		[self nextButtonPressed];
	}

}
- (void) setupWithQuestion:(QuestionModel*)question
{
	for (int i=0; i< [question.anwserArray count];i++ )
	{
		NSDictionary *dict = [question.anwserArray objectAtIndex:i];
		int key = [[dict objectForKey:@"answer"] intValue];
		if (key == 1)
		{
			
			keyCorrect = i+1;			
		}
	}
	NSLog(@"corect = %d",keyCorrect);
	[questionSubjectLabel setText:question.subject];	
	[optionALabel setText:[question.anwserKeyArray objectAtIndex:0]];
	[optionBLabel setText:[question.anwserKeyArray objectAtIndex:1]];
	[optionCLabel setText:[question.anwserKeyArray objectAtIndex:2]];
	[optionDLabel setText:[question.anwserKeyArray objectAtIndex:3]];
}
- (IBAction) optionButtonPressed:(id)sender
{
	[Audio playButtonClicked];
	UIButton *button = (UIButton*) sender;
	switch (button.tag) {
		case 1:
			keySelected = 1;
			[aAnwserButton setSelected:YES];
			[bAnwserButton setSelected:NO];
			[cAnwserButton setSelected:NO];
			[dAnwserButton setSelected:NO];
			break;
		case 2:
			keySelected = 2;
			[aAnwserButton setSelected:NO];
			[bAnwserButton setSelected:YES];
			[cAnwserButton setSelected:NO];
			[dAnwserButton setSelected:NO];
			break;
		case 3:
			keySelected = 3;
			[aAnwserButton setSelected:NO];
			[bAnwserButton setSelected:NO];
			[cAnwserButton setSelected:YES];
			[dAnwserButton setSelected:NO];
			break;
		case 4:
			keySelected = 4;
			[aAnwserButton setSelected:NO];
			[bAnwserButton setSelected:NO];
			[cAnwserButton setSelected:NO];
			[dAnwserButton setSelected:YES];
			break;
			
		default:
			break;
	}
	isChecked = YES;
}
- (IBAction) nextButtonPressed
{	
	[Audio playButtonClicked];
	if (expiredTime>0)
	{
		[playTimer invalidate];
	}
	if (keySelected == keyCorrect)
	{
		mainiPhoneAppDelegate.numOfCorrect++;
	}
	if ([mainiPhoneAppDelegate.arrayGames count] > mainiPhoneAppDelegate.latestQuestionIndex ) 
	{
		GameViewController *controller = [[GameViewController alloc] init];
		[[[HomeGameViewController homeGameViewController] homeGameNavController] popToRootViewControllerAnimated:NO];
		[[[HomeGameViewController homeGameViewController] homeGameNavController] pushViewController:controller animated:YES];
		[controller release];
	}
	else {
		[Audio playWin];
		EndGameViewController *controller = [[EndGameViewController alloc] init];
		[[[HomeGameViewController homeGameViewController] homeGameNavController] popToRootViewControllerAnimated:NO];
		[[[HomeGameViewController homeGameViewController] homeGameNavController] pushViewController:controller animated:YES];
		[controller release];
	}
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	NSLog(@"Game View dealloc..");
    [super dealloc];
}


@end
